'use strict'

let baseUrl = 'http://localhost:8000/blog'

$(function () {
    fn_list()
})

// 渲染数据
function fn_list(baseData) {
    $.get(`${baseUrl}`, data => {
        if (baseData) {
            data = baseData
        }
        $('.row').remove()
        data.forEach(item => {
            let html = `
                    <tr class="row">
                        <td>${item.id}</td>
                        <td>${item.title}</td>
                        <td>${item.summary}</td>
                        <td>${item.content}</td>
                        <td>${item.sort}</td>
                        <td>${item.author}</td>
                        <td>${item.createdAt}</td>
                        <td>
                            <input type="button" value="编辑" onclick="edit(${item.id})">
                            <input type="button" value="删除" onclick="del(${item.id})">
                        </td>
                    </tr>
                    `
            $('#tbData').append(html)
        });
    })
}

// 查询
function query() {
    let keyword = $('#keyword').val()
    $.get(`${baseUrl}?keyword=${keyword}`, data => {
        fn_list(data)
    })
}

// 删除
function del(id) {
    if (confirm('确认删除吗？')) {
        $.ajax({
            url: `${baseUrl}/${id}`,
            type: 'delete',
            success: data => {
                location.href = './index.html'
            }
        })
    }
}

// 编辑
function edit(id) {
    window.location.href = `addOrEdit.html?${id}`
}
// 新增
function add() {
    window.location.href = `addOrEdit.html`
}

// 获取编辑的id
let id = location.search.split('?')[1];

// 判断是不是编辑
if (id) {
    $.get(`http://localhost:8000/blog/${id}`, data => {
        console.log(data);
        $('[name=id]').val(data["id"]);
        $('[name=title]').val(data["title"]);
        $('[name=summary]').val(data["summary"]);
        $('[name=content]').val(data["content"]);
        $('[name=sort]').val(data["sort"]);
        $('[name=author]').val(data["author"]);
    })
}

// 保存
function save() {
    let title = $('[name=title]').val();
    let summary = $('[name=summary]').val();
    let content = $('[name=content]').val();
    let sort = $('[name=sort]').val();
    let author = $('[name=author]').val();
    let obj = {
        title,
        summary,
        content,
        sort,
        author
    }

    if ($('[name=id]').val()) {
        obj.id = $('[name=id]').val();
        $.ajax({
            url: `http://localhost:8000/blog`,
            type: 'put',
            data: obj,
            success: res => {
                console.log(res);
                if (res.code = 1000) {
                    window.location.href = 'index.html'
                }
            }
        })
    } else {
        $.post('http://localhost:8000/blog', obj, res => {
            console.log(res);
            if (res.code = 1000) {
                window.location.href = 'index.html'
            }
        })
    }
}

//取消
function cancel() {
    window.location.href = 'index.html'
}